package com.xh.one2one.lesson.controller;

import com.xh.core.util.UserUtils;
import com.xh.one2one.lesson.bean.dto.LessonSummaryDto;
import com.xh.one2one.lesson.bean.dto.StudentReportStatisDto;
import com.xh.one2one.lesson.bean.request.LessonSummaryDetailsRequest;
import com.xh.one2one.lesson.service.ILessonSummaryService;
import com.xh.one2one.lesson.model.LessonSummary;
import com.xh.one2one.lesson.bean.request.LessonSummaryRequest;
import com.xh.core.bean.XhExportHeaderBean;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.xh.core.bean.AjaxResult;
import com.xh.core.bean.XhPage;
import com.xh.core.constant.AjaxResultConstant;
import org.springframework.validation.annotation.Validated;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;
import java.util.List;

import org.springframework.web.bind.annotation.RestController;
import com.xh.core.controller.BaseController;

/**
 * <p>
 * 教学总结 前端控制器
 * </p>
 *
 * @author TW
 * @since 2021-05-06
 */
@RestController
@RequestMapping("/admin/lessonSummary")
public class LessonSummaryController extends BaseController {

    @Autowired
    private ILessonSummaryService lessonSummaryService;

    /**
     * 分页数据查询
     *
     * @param page 分页对象
     * @param bean 查询条件Bean
     * @return
     */
    @PostMapping("/queryPage")
    public AjaxResult queryPage(XhPage page, LessonSummaryRequest bean) {
        page = lessonSummaryService.queryLessonSummaryPage(page, bean);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, "", page);
    }

    /**
     * 数据查询
     *
     * @param bean 查询条件Bean
     * @return
     */
    @PostMapping("/query")
    public AjaxResult query(LessonSummaryRequest bean) {
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, "", lessonSummaryService.queryLessonSummary(bean));
    }

    /**
     * 数据查询
     *
     * @param bean 查询条件Bean
     * @return
     */
    @PostMapping("/queryLast")
    public AjaxResult queryLast(LessonSummaryRequest bean) {
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, "", lessonSummaryService.queryLast(bean));
    }

    /**
     * 数据查询
     *
     * @param bean 查询条件Bean
     * @return
     */
    @PostMapping("/querySummaryByStudentId")
    public AjaxResult querySummaryByStudentId(LessonSummaryRequest bean) {
        bean.setCreatorId(UserUtils.getCurrentUserId());
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, "", lessonSummaryService.querySummaryByStudentId(bean));
    }

    /**
     * 数据查询
     *
     * @param bean 查询条件Bean
     * @return
     */
    @PostMapping("/querySummaryToReport")
    public AjaxResult querySummaryToReport(LessonSummaryRequest bean) {
        bean.setCreatorId(UserUtils.getCurrentUserId());
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, "", lessonSummaryService.querySummaryToReport(bean));
    }

    /**
     * 查询学生总结
     *
     * @param bean 查询条件Bean
     * @return
     */
    @PostMapping("/queryStudentSummary")
    public AjaxResult queryStudentSummary(LessonSummaryRequest bean) {
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, "", lessonSummaryService.queryStudentSummary(bean));
    }

    /**
     * 小程序学生学习报告
     *
     * @param bean 查询条件Bean
     * @return
     */
    @PostMapping("/queryStudentReport")
    public AjaxResult queryStudentReport(LessonSummaryRequest bean) {
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, "", lessonSummaryService.queryStudentReport(bean));
    }

    /**
     * 导出全部
     *
     * @param bean
     * @return
     */
    @PostMapping(value = "/exportAll")
    public AjaxResult exportAll(LessonSummaryRequest bean, HttpServletRequest request) {
        try {
            LinkedHashMap<String, XhExportHeaderBean> exportHeaderBeans = new LinkedHashMap<>();
            //exportHeaderBeans.put("username",new XhExportHeaderBean("用户名"));
            //exportHeaderBeans.put("deleted",new XhExportHeaderBean("是否删除",ImmutableMap.of(0,"否",1,"是")));
            //exportHeaderBeans.put("createTime",new XhExportHeaderBean("创建时间","yyyy-MM-dd HH:mm:ss"));

            String downloadUrl = lessonSummaryService.exportAll(request, exportHeaderBeans, bean, "queryLessonSummaryPage");
            if (downloadUrl != null) {
                return new AjaxResult(AjaxResultConstant.Result.SUCCESS, "", downloadUrl);
            } else {
                return new AjaxResult(AjaxResultConstant.Result.ERROR, "导出失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(AjaxResultConstant.Result.ERROR);
        }
    }

    /**
     * 新增和保存
     *
     * @param summaryRequest 实体对象
     * @return
     */
    @PostMapping(value = "/saveOrUpdate")
    @Transactional
    public AjaxResult saveOrUpdate(@Validated LessonSummaryRequest summaryRequest) {
        if (summaryRequest.getId() == null) {
            LessonSummary lessonSummary = new LessonSummary();
            BeanUtils.copyProperties(summaryRequest, lessonSummary);
            lessonSummaryService.insert(lessonSummary);
        } else {
//            lessonSummaryService.update(lessonSummary);
            lessonSummaryService.updateLessonSummary(summaryRequest);
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 修改分数
     * @author 赵文博
     * @date 2021/6/21 16:41
     * @param lessonSummary
     * @return com.xh.core.bean.AjaxResult
     **/
    @PostMapping(value = "/updateScore")
    public AjaxResult updateScore(@Validated LessonSummary lessonSummary) {
        boolean flag = lessonSummaryService.updateScore(lessonSummary);
        return flag ? new AjaxResult(AjaxResultConstant.Result.SUCCESS):new AjaxResult(AjaxResultConstant.Result.ERROR);
    }

    /**
     * 删除操作
     *
     * @param id
     * @return
     */
    @PostMapping("/deleteById/{id}")
    @Transactional
    public AjaxResult deleteById(@PathVariable String id) {
        lessonSummaryService.delete(id);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 批量删除操作
     *
     * @param ids
     * @return
     */
    @PostMapping("/batchDelete")
    @Transactional
    public AjaxResult batchDelete(String[] ids) {
        lessonSummaryService.batchDelete(ids);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 报告统计
     *
     * @param bean
     * @return com.xh.core.bean.AjaxResult
     * @author 赵文博
     * @date 2021/6/7 15:02
     **/
    @GetMapping("/reportStatics")
    public AjaxResult queryStudentReportStatics(LessonSummaryRequest bean) {
        try {
            StudentReportStatisDto studentReportStatisDto = lessonSummaryService.queryStudentReportStatis(bean);
            return new AjaxResult(AjaxResultConstant.Result.SUCCESS, studentReportStatisDto);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(AjaxResultConstant.Result.ERROR);
        }
    }

    /**
     * 获取学生课堂报告
     *
     * @param bean
     * @return com.xh.core.bean.AjaxResult
     * @author 赵文博
     * @date 2021/6/8 14:17
     **/
    @GetMapping("/getStudentClassReport")
    public AjaxResult queryStudentClassReport(LessonSummaryRequest bean) {
        LessonSummary summary = lessonSummaryService.queryStudentClassReport(bean);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, summary);
    }

    /**
     * 查询课堂作业
     *
     * @param bean
     * @return com.xh.core.bean.AjaxResult
     * @author 赵文博
     * @date 2021/6/15 17:18
     **/
    @GetMapping("/getClassroomPractice")
    public AjaxResult queryClassroomPractice(LessonSummaryRequest bean) {
        List<LessonSummaryDto> lessonSummaryDtos = lessonSummaryService.queryClassroomPractice(bean);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, lessonSummaryDtos);
    }

    @RequestMapping("/queryStudentWrongQuestions")
    public AjaxResult queryStudentWrongQuestions(XhPage<LessonSummaryDto> page, LessonSummaryDetailsRequest bean) {

        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, lessonSummaryService.queryStudentWrongQuestions(page, bean));
    }
}

